package mouseAndKeyAndScroll;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

// 键盘事件
public class KeyDemo extends Application {
	private Pane root = new Pane();
	private TextField tf;
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		
		tf = new TextField();
		tf.setLayoutX(130);
		tf.setLayoutY(150);
		root.getChildren().add(tf);
		
		Scene scene = new Scene(root, 400, 300);
		stage.setScene(scene);
		stage.show();
		
		// 执行获得焦点的结点的键盘事件
		// 键盘输入检测(长按会一直执行,可加一个条件使执行固定的次数)
		tf.setOnKeyPressed(e -> {
			if(e.getCode() == KeyCode.A) {
				System.out.println("按键为"+ e.getCode() +", 正确");
			} else {
				System.out.println("按键为"+ e.getCode() +", 错误");
			}
		});
				
//		// 检测输入框输入的字符
//		tf.setOnKeyTyped(e -> System.out.print(e.getCharacter()) );
	}
}
